package cn.community.system.service.impl;

import cn.community.system.domain.Association;
import cn.community.system.domain.Device;
import cn.community.system.domain.DeviceRecord;
import cn.community.system.exception.LogicException;
import cn.community.system.mapper.AssociationMapper;
import cn.community.system.mapper.DeviceMapper;
import cn.community.system.mapper.DeviceRecordMapper;
import cn.community.system.query.DeviceRecordQuery;
import cn.community.system.query.QueryObject;
import cn.community.system.service.IDeviceRecordService;
import cn.community.system.util.UserContext;
import cn.community.system.vo.ApplyLendVo;
import cn.community.system.vo.DeviceVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class DeviceRecordServiceImpl implements IDeviceRecordService {
    @Autowired
    private DeviceRecordMapper deviceRecordMapper;

    @Autowired
    private AssociationMapper associationMapper;

    @Autowired
    private DeviceMapper deviceMapper;

    @Override
    public void delete(Long id) {
        // 回滚库存
        DeviceRecord deviceRecord = this.get(id);
        Integer number = deviceRecord.getNumber();
        Long deviceId = deviceRecord.getDeviceId();
        if(deviceRecord.getIsAgree()==1 && deviceRecord.getIsReturn()!=1){ // 已经减少库存，需要回补库存
            deviceMapper.increateNumber(deviceId,number);
        }

        deviceRecordMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void save(DeviceRecord deviceRecordRecord) {
        // 设备id
        String deviceName = deviceRecordRecord.getDeviceName();
        Device device = deviceMapper.listByName(deviceName);
        if(device == null){
            throw new LogicException("请输入正确的设备！");
        }
        deviceRecordRecord.setDeviceId(device.getId());
        // 判断借用数量是否合格
        Integer number = deviceRecordRecord.getNumber();
        Integer nowNumber = device.getNowNumber();
        if(number > nowNumber){
            throw new LogicException("借用数量大于库存现有数量！");
        }
        // 社团名称
        String association = deviceRecordRecord.getAssociation();
        Association association1 = associationMapper.selectByName(association);
        if (association1 == null){
            throw new LogicException("请输入正确的社团！");
        }
        deviceRecordRecord.setAssociationId(association1.getId());
        deviceRecordRecord.setApplyTime(new Date());
        deviceRecordMapper.insert(deviceRecordRecord);
    }

    @Override
    public DeviceRecord get(Long id) {
        return deviceRecordMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<DeviceRecord> listAll() {
        return deviceRecordMapper.selectAll();
    }

    @Override
    public void update(DeviceRecord deviceRecordRecord) {
        // 设备id
        String deviceName = deviceRecordRecord.getDeviceName();
        Device device = deviceMapper.listByName(deviceName);
        if(device == null){
            throw new LogicException("请输入正确的设备！");
        }
        deviceRecordRecord.setDeviceId(device.getId());
        // 判断借用数量是否合格
        Integer number = deviceRecordRecord.getNumber();
        Integer nowNumber = device.getNowNumber();
        if(number > nowNumber){
            throw new LogicException("借用数量大于库存现有数量！");
        }
        // 社团名称
        String association = deviceRecordRecord.getAssociation();
        Association association1 = associationMapper.selectByName(association);
        if (association1 == null){
            throw new LogicException("请输入正确的社团！");
        }
        deviceRecordRecord.setAssociationId(association1.getId());
        deviceRecordRecord.setApplyTime(new Date());
        deviceRecordMapper.updateByPrimaryKey(deviceRecordRecord);
    }

    public PageInfo query(QueryObject qo) {
        //使用分页插件,传入当前页,每页显示数量
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        //查询结果集
        List<DeviceRecord> list = deviceRecordMapper.selectForList(qo);
        return new PageInfo(list);
    }

    public PageInfo queryAsso(DeviceRecordQuery qo) {
        Association association = UserContext.getCurrentAssociation();
        qo.setAssoId(association.getId());
        //使用分页插件,传入当前页,每页显示数量
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(),"applyTime desc");
        //查询结果集
        List<DeviceRecord> list = deviceRecordMapper.selectForAssoList(qo);
        return new PageInfo(list);
    }

    @Override
    public void agreeLeading(DeviceRecord deviceRecord) {
        Integer isAgree = deviceRecord.getIsAgree();
        if (isAgree==1){
            deviceRecord.setLendingTime(new Date());
            deviceRecord.setIsReturn(3);
            // 减少库存
            Long deviceId = deviceRecord.getDeviceId();
            Integer number = deviceRecord.getNumber();
            deviceMapper.decreateNumber(deviceId,number);
        }else {
            deviceRecord.setIsReturn(null);
        }
        deviceRecordMapper.agreeLeading(deviceRecord);
    }

    @Override
    public void agreeReturn(DeviceRecord deviceRecord) {
        if(deviceRecord.getIsAgree() != 1){
            throw new LogicException("未能成功借用该设备，无需审核！");
        }
        Integer isReturn = deviceRecord.getIsReturn();
        if (isReturn==1){
            deviceRecord.setReturnTime(new Date());
            // 增加库存
            Long deviceId = deviceRecord.getDeviceId();
            Integer number = deviceRecord.getNumber();
            deviceMapper.increateNumber(deviceId,number);
        }
        deviceRecordMapper.agreeReturn(deviceRecord);
    }

    @Override
    public void applyReturn(Integer isReturn, Long id) {
        DeviceRecord deviceRecord = new DeviceRecord();
        deviceRecord.setId(id);
        deviceRecord.setIsReturn(isReturn);
        deviceRecord.setApplyTime(new Date());
        deviceRecordMapper.applyReturn(deviceRecord);
    }


    @Override
    public PageInfo queryLeadList(DeviceRecordQuery qo) {
        //使用分页插件,传入当前页,每页显示数量
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        //查询结果集
        List<DeviceVo> list = deviceRecordMapper.queryLeadList(qo);
        return new PageInfo(list);
    }

    @Override
    public PageInfo queryReturnList(DeviceRecordQuery qo) {
        //使用分页插件,传入当前页,每页显示数量
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        //查询结果集
        List<DeviceVo> list = deviceRecordMapper.queryReturnList(qo);
        return new PageInfo(list);
    }

    @Override
    public PageInfo queryLeadListAsso(DeviceRecordQuery qo) {
        Association association = UserContext.getCurrentAssociation();
        qo.setAssoId(association.getId());
        //使用分页插件,传入当前页,每页显示数量
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(),"id desc");
        //查询结果集
        List<DeviceVo> list = deviceRecordMapper.queryLeadListAsso(qo);
        return new PageInfo(list);
    }

    @Override
    public PageInfo queryReturnListAsso(DeviceRecordQuery qo) {
        Association association = UserContext.getCurrentAssociation();
        qo.setAssoId(association.getId());
        //使用分页插件,传入当前页,每页显示数量
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(),"id desc");
        //查询结果集
        List<DeviceVo> list = deviceRecordMapper.queryReturnListAsso(qo);
        return new PageInfo(list);
    }

    @Override
    public void saveLend(ApplyLendVo applyLendVo) {
        // 判断：借出数量是否大于现存数量
        if(applyLendVo.getNumber() > applyLendVo.getNowNumber()){
            throw new LogicException("现存的数量不足以借用这么多！");
        }
        // 保存借出记录
        DeviceRecord deviceRecord = new DeviceRecord();
        deviceRecord.setDeviceId(applyLendVo.getDeviceId());
        deviceRecord.setDeviceName(applyLendVo.getName());
        Long id = applyLendVo.getAssociationId();
        deviceRecord.setAssociationId(id);
        String username = associationMapper.selectByPrimaryKey(id).getUsername();
        deviceRecord.setAssociation(username);
        deviceRecord.setIsAgree(DeviceRecord.APPLYING);
        deviceRecord.setReason(applyLendVo.getReason());
        deviceRecord.setDutyName(applyLendVo.getDutyName());
        deviceRecord.setMobile(applyLendVo.getMobile());
        deviceRecord.setNumber(applyLendVo.getNumber());
        deviceRecord.setApplyTime(new Date());

        deviceRecordMapper.insert(deviceRecord);
    }

    @Override
    public void saveLendAsso(ApplyLendVo applyLendVo) {
        // 判断：借出数量是否大于现存数量
        if(applyLendVo.getNumber() > applyLendVo.getNowNumber()){
            throw new LogicException("现存的数量不足以借用这么多！");
        }
        // 保存借出记录
        DeviceRecord deviceRecord = new DeviceRecord();
        deviceRecord.setDeviceId(applyLendVo.getDeviceId());
        deviceRecord.setDeviceName(applyLendVo.getName());
        Association association = UserContext.getCurrentAssociation();
        deviceRecord.setAssociationId(association.getId());
        deviceRecord.setAssociation(association.getUsername());
        deviceRecord.setIsAgree(DeviceRecord.APPLYING);
        deviceRecord.setReason(applyLendVo.getReason());
        deviceRecord.setDutyName(applyLendVo.getDutyName());
        deviceRecord.setMobile(applyLendVo.getMobile());
        deviceRecord.setNumber(applyLendVo.getNumber());
        deviceRecord.setApplyTime(new Date());

        deviceRecordMapper.insert(deviceRecord);
    }

}
